package com.nut.course.service.impl;

import com.nut.course.api.resp.UserAuth;
import com.nut.course.common.Result;
import com.nut.course.entity.User;
import com.nut.course.interceptor.JwtTokenUtil;
import com.nut.course.interceptor.UserJwt;
import com.nut.course.mapper.BizUserMapper;
import com.nut.course.service.UserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private WxCpService wxCpService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private BizUserMapper bizUserMapper;

    @Override
    public Result<UserAuth> login(String code, String state) {

        WxCpOauth2UserInfo userInfo;
        try {

            userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }

        log.info("userInfo=" + userInfo.toString());
        String userId = userInfo.getUserId();
        String openId = userInfo.getOpenId();

        log.info("userId=" + userId);

        //获取用户姓名
        String username = null;
        User user = bizUserMapper.selectById(userId);
        if (user == null) {
            Result<WxCpUser> cpUserResult = this.getUserInfo(userId);
            if (cpUserResult.isSuccess()) {
                WxCpUser cpUser = cpUserResult.getResult();
                username = cpUser.getName();
            }

            User newUser = new User();
            newUser.setId(userId);
            newUser.setName(username);
            newUser.setOpenId(openId);
            newUser.setStatus(0);
            bizUserMapper.insert(newUser);
        } else {
            username = user.getName();
        }

        UserJwt userJwt = new UserJwt();
        userJwt.setUserId(userId);
        userJwt.setUsername(username);

        String token = jwtTokenUtil.generateToken(userJwt);

        UserAuth userAuth = new UserAuth();
        userAuth.setToken(token);

        return Result.ok(userAuth);
    }

    @Override
    public Result<WxCpUser> getUserInfo(String userId) {
        WxCpUser wxCpUser = null;
        try {
            wxCpUser = wxCpService.getUserService().getById(userId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        log.info("wxCpUser=" + wxCpUser.toJson());
        return Result.ok(wxCpUser);
    }

    @Override
    public Result<List<WxCpUser>> getUsersByDepartId(Long departId) {

        List<WxCpUser> wxCpUsers = null;
        try {
            wxCpUsers = wxCpService.getUserService().listSimpleByDepartment(departId, false, null);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        log.info("wxCpUsers=" + wxCpUsers.toArray());
        return Result.ok(wxCpUsers);
    }


}
